//
//  LatestRecommendTableViewCell.m
//  HAODE
//
//  Created by LiYinDi on 15/12/30.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "LatestRecommendTableViewCell.h"
#import "UIImageView+WebCache.h"

@implementation LatestRecommendTableViewCell

- (void)awakeFromNib {
    // Initialization code
    [self setAutoLayout];
}
-(void)setModel:(LatestRecommendModel *)model
{
    _infoTitleLabel.text=model.infoTitle;
    
    
    NSString *timeStr=model.createDate;
    NSString *curStr=[self GetCurTime];
    double interval=[self GetStringTimeDiff:timeStr timeE:curStr];
    int hour=interval/3600;
    int minute=(int)interval % 3600 / 60;
    int day=(int)interval/3600/24;
    
    if (day!=0&&day>0) {
        _createDateLabel.text=[NSString stringWithFormat:@"%d天前",day];
    }
    else if((day==0||day<0)&&hour>0)
    {
        _createDateLabel.text=[NSString stringWithFormat:@"%d小时前",hour];
    }
    else if ((day<=0&&hour<=0)&&minute>=0) {
        _createDateLabel.text=[NSString stringWithFormat:@"%d分前",minute];
    }
    
    NSString *urlStr=model.imageUrl;
    if (urlStr.length>0) {
        
        [_imageUrlImageView setImageWithURL:[NSURL URLWithString:urlStr]];
    }
    else
    {
        [_imageUrlImageView setImage:[UIImage imageNamed:@"1024x1024logos"]];
    }


}
- (double)GetStringTimeDiff:(NSString*)timeS timeE:(NSString*)timeE

{
    
    double timeDiff = 0.0;
    
    NSDateFormatter *formatters = [[NSDateFormatter alloc] init];
    [formatters setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateS = [formatters dateFromString:timeS];
    
    
    
    NSDateFormatter *formatterE = [[NSDateFormatter alloc] init];
    [formatterE setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateE = [formatterE dateFromString:timeE];
    
    
    timeDiff = [dateE timeIntervalSinceDate:dateS ];
    
    return timeDiff;
    
}

- (NSString*)GetCurTime

{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSString*timeString=[formatter stringFromDate: [NSDate date]];
    
    
    return timeString;
}

-(void)setAutoLayout
{
    [_createDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).with.offset(17);
        make.height.equalTo(@16);
        make.width.equalTo(@64);
    }];
    
    [_frameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(_createDateLabel.mas_bottom).with.offset(15);
        
        make.left.equalTo(self.mas_left).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.height.equalTo(_frameView.mas_width).dividedBy(1.67);
    }];
    
    [_imageUrlImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_frameView.mas_top).with.offset(3);
         make.left.equalTo(_frameView.mas_left).with.offset(3);
         make.right.equalTo(_frameView.mas_right).with.offset(-3);
//        make.height.equalTo(_frameView.mas_height).with.offset(-40);
        make.bottom.equalTo(_infoTitleLabel.mas_top).with.offset(-15);
    }];
    
    [_infoTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
         make.top.equalTo(_imageUrlImageView.mas_bottom).with.offset(15);
        make.bottom.equalTo(_frameView.mas_bottom).with.offset(-15);
        make.left.equalTo(_frameView.mas_left).with.offset(9) ;
        make.right.equalTo(_imageUrlImageView.mas_right) ;
        make.height.equalTo(@15) ;
    }];
//    [self mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(_frameView.mas_height).with.offset(30);
//    }];
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
